package com.wf.smartcomm.server.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;


import com.wf.smartcomm.server.apns.IApnsService;
import com.wf.smartcomm.server.apns.impl.ApnsServiceImpl;
import com.wf.smartcomm.server.apns.model.ApnsConfig;
import com.wf.smartcomm.server.push.Pusher;



public class PushUtils {
	
	public static Properties pushConfig = new Properties();
	private static String serverIp;
	private static String clientPushServer;
	private static String port;
	private static int pushThreadPoolSize;
	public static boolean isDevEnv;
	private static String password;
	private static ApnsConfig config = new ApnsConfig();
	private static Logger logger = Logger.getLogger(PushUtils.class);
	
	static{
		InputStream in = PushUtils.class.getClassLoader().getResourceAsStream("smartcomm.properties");
		InputStream is = PushUtils.class.getClassLoader().getResourceAsStream("push.p12");
		try {
			pushConfig.load(in);
			serverIp = pushConfig.getProperty("ServerIp");
			clientPushServer = pushConfig.getProperty("ClientPushServer");
			port = pushConfig.getProperty("Port");
			isDevEnv = Boolean.parseBoolean(pushConfig.getProperty("isDevEnv"));
			password = pushConfig.getProperty("cerPassword");
			pushThreadPoolSize = Integer.parseInt(pushConfig.getProperty("pushThreadPoolSize"));
			config.setKeyStore(is);
			config.setDevEnv(isDevEnv);
			config.setPassword(password);
			config.setPoolSize(pushThreadPoolSize);
			in.close();
			is.close();
		} catch (IOException e) {
			logger.error(e.getMessage(),e);
			throw new RuntimeException(e);
		}
	}
	
	public static Pusher getInstance() throws Exception{
		return new Pusher(serverIp, Integer.parseInt(port), 5000);
	}
	
	public static Pusher getClientPusher() throws Exception{
		return new Pusher(clientPushServer, Integer.parseInt(port), 5000);
	}
	
	private static IApnsService apnsService;
	
	public static IApnsService getApnsService() {
		if (apnsService == null) {
			apnsService = ApnsServiceImpl.createInstance(config);
		}
		return apnsService;
	}
	
}
